package com.inspur.myapplication.view;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.inspur.myapplication.R;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class WeatherActivity extends AppCompatActivity {
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather);
        textView=(TextView)findViewById(R.id.weather_tv);
    }

    @Override
    protected void onStart() {
        super.onStart();
        new Thread(new MyHttpReq()).start();

    }

    //对于网络请求等耗时操作，需要单独启动线程运行。
    class MyHttpReq implements Runnable{
        @Override
        public void run() {
            try{
                URL url=new URL("http://v.juhe.cn/weather/index?format=2&cityname=%E8%8B%8F%E5%B7%9E&key=3da3335a734af2faddf5915ada3dad5e");
                HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
                //设置连接属性
                httpURLConnection.setRequestMethod("GET");
                httpURLConnection.setConnectTimeout(100000);
                httpURLConnection.setDoInput(true);
                httpURLConnection.setDoOutput(true);
                //建立连接
                httpURLConnection.connect();
                //判断服务器的响应码是否正常
                if(httpURLConnection.getResponseCode()!=HttpURLConnection.HTTP_BAD_REQUEST){
                    //通过输入流获取信息
                    BufferedReader bufferedReader=new BufferedReader( new InputStreamReader( httpURLConnection.getInputStream()));
                    String line="";
                    StringBuffer all=new StringBuffer();
                    while ((line=bufferedReader.readLine())!=null){
                        all.append(line);
                    }
                    String content=all.toString();
                    Log.d(MyHttpReq.class.toString(),content);
                    bufferedReader.close();

                    //解析JSON数据
                    JSONObject jsonObject=new JSONObject(content);
                    String resultcode=jsonObject.getString("resultcode");
                    if(resultcode.equals("200")){
                        jsonObject=jsonObject.getJSONObject("result");
                        jsonObject=jsonObject.getJSONObject("sk");
                        final String temp=jsonObject.getString("temp");
                        Log.d(MyHttpReq.class.toString(),temp);
                        //textView.setText(temp);
                        //其他线程的代码无法修改主线程的界面
                        textView.post(new Runnable() {//post方法可以让它的参数中的代码，运行在主线程中
                            @Override
                            public void run() {
                                textView.setText(temp);
                            }
                        });
                    }
                }
                //断开连接
                httpURLConnection.disconnect();

            }catch (Exception e){
                Log.e(MyHttpReq.class.toString(),e.toString());
            }

        }
    }
}
